
/**
 * Buffer
 * Buffer类型用于存储原始数据，与整型数组类似
 * 使用Buffer类型，是因为JavaScript语言没有二进制类型
 * 所以，当处理TCP流时或者文件系统时，很必要。
 * 
 * StringDecoder
 * 用于把buffer类型解码为string类型
 * 与buffer.toString()方法类似
 */

 const StringDecoder = require('string_decoder').StringDecoder;

 const decoder = new StringDecoder('utf8');

 var bufNum = new Buffer.alloc(10); // 开辟10个字节的buffer
 var bufArray = new Buffer.from([10, 20, 30, 40]); // 
 var bufString = new Buffer.from('this is test'); // 开辟一个buffer，存储提供的字符串
 console.log(decoder.write(bufString));

 var len = bufNum.write('test');
 console.log(bufNum);
 console.log(bufNum.toString());

 var buf2 = new Buffer('1234567890', 'hex');
 console.log(decoder.write(buf2));